<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\RedisModel;
use app\model\ShopsModel;
use think\App;

class Shops extends Base {

    protected $middleware = [Auth::class];

    private $aid;

    private $shopsModel;

    private $req;

    private $page;

    private $limit;

    private $shop_id;

    private $shopIds;

    public $cacheKey="shop:id:";


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->shop_id = isset($this->req['shop_id']) ? $this->req['shop_id'] : "";

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 20;

        $this->shopsModel = new ShopsModel();

        $this->aid = $this->isLogin();

        $this->shopIds = $this->shopsModel->getShopIds($this->aid);

    }



    /*
     * 获取店铺列表
     * */
    public function getList(){
        $where = [];
        $where[] = ['is_delete',"=",0];
        $where[] = ['shop_id','in',$this->shopIds];
        if(!empty($this->req['keyword'])){
            $where[] = ['shop_name|contacter|company_name','like',"%".$this->req['keyword']."%"];
        }

        if(isset($this->req["shop_status"]) && is_numeric($this->req["shop_status"]) && in_array($this->req["shop_status"],[0,1,-1,-2])){
            $where[] =['shop_status',"=",$this->req["shop_status"]];
        }

        $list = $this->shopsModel->getList($where,$this->page,$this->limit);

        $total =  $this->shopsModel->getTotal($where);

        return $this->succData($total,$list);

    }


    /*
     * 获取店铺详情
     * */
    public function getShopInfo(){

        if(empty($this->shop_id)){
            return $this->fail("店铺id不能为空");
        }

        if(!in_array($this->shop_id,$this->shopIds)){
            return $this->fail("店铺不存在");
        }

        $info = $this->shopsModel->getInfo($this->shop_id);

        return $this->succData($info);
    }



    public function add()
    {

        if(mb_strlen($this->req['shop_name']) > 20 || empty($this->req['shop_name']) ){
            return $this->fail("店铺名称不合法");
        }

        if(empty($this->req['contacter'])){
            return $this->fail("联系人不能为空");
        }

        if(empty($this->req['contact_address'])){
            return $this->fail("联系地址不能为空");
        }

        if(!verification_mobile($this->req['contact_phone'])){
            return $this->fail("联系人电话不合法");
        }

        if(empty($this->req['company_name'])){
            return $this->fail("公司名称不能为空");
        }

        if(empty($this->req['company_tel'])){
           // return $this->fail("公司电话不能为空");
        }


        unset($this->req['product_num'],$this->req['return_addresses_num'],$this->req['send_addresses_num']);

        $this->req['updated_at'] = date("Y-m-d H:i:s",time());

       if( $this->shopsModel->addOne($this->req)){

           if(isset($this->req["shop_id"])){
               $this->delCache($this->req["shop_id"]);
           }

           return $this->succ();

       }else{

           return $this->fail();

       }
    }


    /*
     * 店铺审核
     * */
    public function check(){
        if(empty($this->shop_id)){
            return $this->fail("店铺id不能为空");
        }

        if(empty($this->req['shop_status'])){
            return $this->fail("审核状态不能为空");
        }

        if(!in_array($this->shop_id,$this->shopIds)){
            return $this->fail("店铺不存在");
        }

        if(!in_array($this->req['shop_status'],[1,-1,-2])){
            return $this->fail("状态不合法");
        }

        if($this->shopsModel->updateShopStatus($this->shop_id,$this->req['shop_status'])){

            $this->delCache($this->shop_id);

            return $this->succ();

        }else{

            return $this->fail();

        }
    }


    /*
     * 删除店铺
     * */
    public function del(){
        if(empty($this->shop_id)){
            return $this->fail("店铺id不能为空");
        }

        if(!in_array($this->shop_id,$this->shopIds)){
            return $this->fail("店铺不存在");
        }

        if($this->shopsModel->delShop($this->shop_id)){

            $this->delCache($this->shop_id);

            return $this->succ();

        }else{

            return $this->fail();

        }
    }



    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$id);
    }

    public function delAllCache()
    {

        $redisModel = new RedisModel();

        return $redisModel->deleteAll($this->cacheKey);
    }

}
